
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 3/29/2013 2:25:55 PM
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace DNNspot.LiveChat.DataModel
{
	/// <summary>
	/// Encapsulates the 'DNNspot_LiveChat_Agents' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(Agent))]	
	[XmlType("Agent")]
	public partial class Agent : esAgent
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new Agent();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Int32 agentId)
		{
			var obj = new Agent();
			obj.AgentId = agentId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Int32 agentId, esSqlAccessType sqlAccessType)
		{
			var obj = new Agent();
			obj.AgentId = agentId;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("AgentCollection")]
	public partial class AgentCollection : esAgentCollection, IEnumerable<Agent>
	{
		public Agent FindByPrimaryKey(System.Int32 agentId)
		{
			return this.SingleOrDefault(e => e.AgentId == agentId);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(Agent))]
		public class AgentCollectionWCFPacket : esCollectionWCFPacket<AgentCollection>
		{
			public static implicit operator AgentCollection(AgentCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator AgentCollectionWCFPacket(AgentCollection collection)
			{
				return new AgentCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class AgentQuery : esAgentQuery
	{
		public AgentQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "AgentQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(AgentQuery query)
		{
			return AgentQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator AgentQuery(string query)
		{
			return (AgentQuery)AgentQuery.SerializeHelper.FromXml(query, typeof(AgentQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esAgent : esEntity
	{
		public esAgent()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Int32 agentId)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(agentId);
			else
				return LoadByPrimaryKeyStoredProcedure(agentId);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Int32 agentId)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(agentId);
			else
				return LoadByPrimaryKeyStoredProcedure(agentId);
		}

		private bool LoadByPrimaryKeyDynamic(System.Int32 agentId)
		{
			AgentQuery query = new AgentQuery();
			query.Where(query.AgentId == agentId);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Int32 agentId)
		{
			esParameters parms = new esParameters();
			parms.Add("AgentId", agentId);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.PortalId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? PortalId
		{
			get
			{
				return base.GetSystemInt32(AgentMetadata.ColumnNames.PortalId);
			}
			
			set
			{
				if(base.SetSystemInt32(AgentMetadata.ColumnNames.PortalId, value))
				{
					OnPropertyChanged(AgentMetadata.PropertyNames.PortalId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.AgentId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? AgentId
		{
			get
			{
				return base.GetSystemInt32(AgentMetadata.ColumnNames.AgentId);
			}
			
			set
			{
				if(base.SetSystemInt32(AgentMetadata.ColumnNames.AgentId, value))
				{
					OnPropertyChanged(AgentMetadata.PropertyNames.AgentId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.UserId
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Int32? UserId
		{
			get
			{
				return base.GetSystemInt32(AgentMetadata.ColumnNames.UserId);
			}
			
			set
			{
				if(base.SetSystemInt32(AgentMetadata.ColumnNames.UserId, value))
				{
					this._UpToUserByUserId = null;
					this.OnPropertyChanged("UpToUserByUserId");
					OnPropertyChanged(AgentMetadata.PropertyNames.UserId);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.IsLoggedIn
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Boolean? IsLoggedIn
		{
			get
			{
				return base.GetSystemBoolean(AgentMetadata.ColumnNames.IsLoggedIn);
			}
			
			set
			{
				if(base.SetSystemBoolean(AgentMetadata.ColumnNames.IsLoggedIn, value))
				{
					OnPropertyChanged(AgentMetadata.PropertyNames.IsLoggedIn);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.CreatedDate
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? CreatedDate
		{
			get
			{
				return base.GetSystemDateTime(AgentMetadata.ColumnNames.CreatedDate);
			}
			
			set
			{
				if(base.SetSystemDateTime(AgentMetadata.ColumnNames.CreatedDate, value))
				{
					OnPropertyChanged(AgentMetadata.PropertyNames.CreatedDate);
				}
			}
		}		
		
		/// <summary>
		/// Maps to DNNspot_LiveChat_Agents.ModifiedDate
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? ModifiedDate
		{
			get
			{
				return base.GetSystemDateTime(AgentMetadata.ColumnNames.ModifiedDate);
			}
			
			set
			{
				if(base.SetSystemDateTime(AgentMetadata.ColumnNames.ModifiedDate, value))
				{
					OnPropertyChanged(AgentMetadata.PropertyNames.ModifiedDate);
				}
			}
		}		
		
		[CLSCompliant(false)]
		internal protected User _UpToUserByUserId;
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "PortalId": this.str().PortalId = (string)value; break;							
						case "AgentId": this.str().AgentId = (string)value; break;							
						case "UserId": this.str().UserId = (string)value; break;							
						case "IsLoggedIn": this.str().IsLoggedIn = (string)value; break;							
						case "CreatedDate": this.str().CreatedDate = (string)value; break;							
						case "ModifiedDate": this.str().ModifiedDate = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "PortalId":
						
							if (value == null || value is System.Int32)
								this.PortalId = (System.Int32?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.PortalId);
							break;
						
						case "AgentId":
						
							if (value == null || value is System.Int32)
								this.AgentId = (System.Int32?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.AgentId);
							break;
						
						case "UserId":
						
							if (value == null || value is System.Int32)
								this.UserId = (System.Int32?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.UserId);
							break;
						
						case "IsLoggedIn":
						
							if (value == null || value is System.Boolean)
								this.IsLoggedIn = (System.Boolean?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.IsLoggedIn);
							break;
						
						case "CreatedDate":
						
							if (value == null || value is System.DateTime)
								this.CreatedDate = (System.DateTime?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.CreatedDate);
							break;
						
						case "ModifiedDate":
						
							if (value == null || value is System.DateTime)
								this.ModifiedDate = (System.DateTime?)value;
								OnPropertyChanged(AgentMetadata.PropertyNames.ModifiedDate);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esAgent entity)
			{
				this.entity = entity;
			}
			
	
			public System.String PortalId
			{
				get
				{
					System.Int32? data = entity.PortalId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.PortalId = null;
					else entity.PortalId = Convert.ToInt32(value);
				}
			}
				
			public System.String AgentId
			{
				get
				{
					System.Int32? data = entity.AgentId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.AgentId = null;
					else entity.AgentId = Convert.ToInt32(value);
				}
			}
				
			public System.String UserId
			{
				get
				{
					System.Int32? data = entity.UserId;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.UserId = null;
					else entity.UserId = Convert.ToInt32(value);
				}
			}
				
			public System.String IsLoggedIn
			{
				get
				{
					System.Boolean? data = entity.IsLoggedIn;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.IsLoggedIn = null;
					else entity.IsLoggedIn = Convert.ToBoolean(value);
				}
			}
				
			public System.String CreatedDate
			{
				get
				{
					System.DateTime? data = entity.CreatedDate;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedDate = null;
					else entity.CreatedDate = Convert.ToDateTime(value);
				}
			}
				
			public System.String ModifiedDate
			{
				get
				{
					System.DateTime? data = entity.ModifiedDate;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.ModifiedDate = null;
					else entity.ModifiedDate = Convert.ToDateTime(value);
				}
			}
			

			private esAgent entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return AgentMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public AgentQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new AgentQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(AgentQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(AgentQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private AgentQuery query;		
	}



	[Serializable]
	abstract public partial class esAgentCollection : esEntityCollection<Agent>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return AgentMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "AgentCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public AgentQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new AgentQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(AgentQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new AgentQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(AgentQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((AgentQuery)query);
		}

		#endregion
		
		private AgentQuery query;
	}



	[Serializable]
	abstract public partial class esAgentQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return AgentMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "PortalId": return this.PortalId;
				case "AgentId": return this.AgentId;
				case "UserId": return this.UserId;
				case "IsLoggedIn": return this.IsLoggedIn;
				case "CreatedDate": return this.CreatedDate;
				case "ModifiedDate": return this.ModifiedDate;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem PortalId
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.PortalId, esSystemType.Int32); }
		} 
		
		public esQueryItem AgentId
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.AgentId, esSystemType.Int32); }
		} 
		
		public esQueryItem UserId
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.UserId, esSystemType.Int32); }
		} 
		
		public esQueryItem IsLoggedIn
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.IsLoggedIn, esSystemType.Boolean); }
		} 
		
		public esQueryItem CreatedDate
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.CreatedDate, esSystemType.DateTime); }
		} 
		
		public esQueryItem ModifiedDate
		{
			get { return new esQueryItem(this, AgentMetadata.ColumnNames.ModifiedDate, esSystemType.DateTime); }
		} 
		
		#endregion
		
	}


	
	public partial class Agent : esAgent
	{

		#region MessageCollectionByAgentId - Zero To Many
		
		static public esPrefetchMap Prefetch_MessageCollectionByAgentId
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = DNNspot.LiveChat.DataModel.Agent.MessageCollectionByAgentId_Delegate;
				map.PropertyName = "MessageCollectionByAgentId";
				map.MyColumnName = "AgentId";
				map.ParentColumnName = "AgentId";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void MessageCollectionByAgentId_Delegate(esPrefetchParameters data)
		{
			AgentQuery parent = new AgentQuery(data.NextAlias());

			MessageQuery me = data.You != null ? data.You as MessageQuery : new MessageQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.AgentId == me.AgentId);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - FK_DNNspot_LiveChat_Messages_DNNspot_LiveChat_Agents
		/// </summary>

		[XmlIgnore]
		public MessageCollection MessageCollectionByAgentId
		{
			get
			{
				if(this._MessageCollectionByAgentId == null)
				{
					this._MessageCollectionByAgentId = new MessageCollection();
					this._MessageCollectionByAgentId.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("MessageCollectionByAgentId", this._MessageCollectionByAgentId);
				
					if (this.AgentId != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._MessageCollectionByAgentId.Query.Where(this._MessageCollectionByAgentId.Query.AgentId == this.AgentId);
							this._MessageCollectionByAgentId.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._MessageCollectionByAgentId.fks.Add(MessageMetadata.ColumnNames.AgentId, this.AgentId);
					}
				}

				return this._MessageCollectionByAgentId;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._MessageCollectionByAgentId != null) 
				{ 
					this.RemovePostSave("MessageCollectionByAgentId"); 
					this._MessageCollectionByAgentId = null;
					
				} 
			} 			
		}
			
		
		private MessageCollection _MessageCollectionByAgentId;
		#endregion

		#region SessionCollectionByAgentId - Zero To Many
		
		static public esPrefetchMap Prefetch_SessionCollectionByAgentId
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = DNNspot.LiveChat.DataModel.Agent.SessionCollectionByAgentId_Delegate;
				map.PropertyName = "SessionCollectionByAgentId";
				map.MyColumnName = "AgentId";
				map.ParentColumnName = "AgentId";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void SessionCollectionByAgentId_Delegate(esPrefetchParameters data)
		{
			AgentQuery parent = new AgentQuery(data.NextAlias());

			SessionQuery me = data.You != null ? data.You as SessionQuery : new SessionQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.AgentId == me.AgentId);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - FK_DNNspot_LiveChat_Sessions_DNNspot_LiveChat_Agents
		/// </summary>

		[XmlIgnore]
		public SessionCollection SessionCollectionByAgentId
		{
			get
			{
				if(this._SessionCollectionByAgentId == null)
				{
					this._SessionCollectionByAgentId = new SessionCollection();
					this._SessionCollectionByAgentId.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("SessionCollectionByAgentId", this._SessionCollectionByAgentId);
				
					if (this.AgentId != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._SessionCollectionByAgentId.Query.Where(this._SessionCollectionByAgentId.Query.AgentId == this.AgentId);
							this._SessionCollectionByAgentId.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._SessionCollectionByAgentId.fks.Add(SessionMetadata.ColumnNames.AgentId, this.AgentId);
					}
				}

				return this._SessionCollectionByAgentId;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._SessionCollectionByAgentId != null) 
				{ 
					this.RemovePostSave("SessionCollectionByAgentId"); 
					this._SessionCollectionByAgentId = null;
					
				} 
			} 			
		}
			
		
		private SessionCollection _SessionCollectionByAgentId;
		#endregion

				
		#region UpToUserByUserId - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK_DNNspot_LiveChat_Agents_Users
		/// </summary>

		[XmlIgnore]
					
		public User UpToUserByUserId
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToUserByUserId == null && UserId != null)
				{
					this._UpToUserByUserId = new User();
					this._UpToUserByUserId.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToUserByUserId", this._UpToUserByUserId);
					this._UpToUserByUserId.Query.Where(this._UpToUserByUserId.Query.UserID == this.UserId);
					this._UpToUserByUserId.Query.Load();
				}	
				return this._UpToUserByUserId;
			}
			
			set
			{
				this.RemovePreSave("UpToUserByUserId");
				

				if(value == null)
				{
					this.UserId = null;
					this._UpToUserByUserId = null;
				}
				else
				{
					this.UserId = value.UserID;
					this._UpToUserByUserId = value;
					this.SetPreSave("UpToUserByUserId", this._UpToUserByUserId);
				}
				
			}
		}
		#endregion
		

		
		protected override esEntityCollectionBase CreateCollectionForPrefetch(string name)
		{
			esEntityCollectionBase coll = null;

			switch (name)
			{
				case "MessageCollectionByAgentId":
					coll = this.MessageCollectionByAgentId;
					break;
				case "SessionCollectionByAgentId":
					coll = this.SessionCollectionByAgentId;
					break;	
			}

			return coll;
		}		
		/// <summary>
		/// Used internally by the entity's hierarchical properties.
		/// </summary>
		protected override List<esPropertyDescriptor> GetHierarchicalProperties()
		{
			List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
			
			props.Add(new esPropertyDescriptor(this, "MessageCollectionByAgentId", typeof(MessageCollection), new Message()));
			props.Add(new esPropertyDescriptor(this, "SessionCollectionByAgentId", typeof(SessionCollection), new Session()));
		
			return props;
		}
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PreSave.
		/// </summary>
		protected override void ApplyPreSaveKeys()
		{
			if(!this.es.IsDeleted && this._UpToUserByUserId != null)
			{
				this.UserId = this._UpToUserByUserId.UserID;
			}
		}
		
		/// <summary>
		/// Called by ApplyPostSaveKeys 
		/// </summary>
		/// <param name="coll">The collection to enumerate over</param>
		/// <param name="key">"The column name</param>
		/// <param name="value">The column value</param>
		private void Apply(esEntityCollectionBase coll, string key, object value)
		{
			foreach (esEntity obj in coll)
			{
				if (obj.es.IsAdded)
				{
					obj.SetProperty(key, value);
				}
			}
		}
		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostSave.
		/// </summary>
		protected override void ApplyPostSaveKeys()
		{
			if(this._MessageCollectionByAgentId != null)
			{
				Apply(this._MessageCollectionByAgentId, "AgentId", this.AgentId);
			}
			if(this._SessionCollectionByAgentId != null)
			{
				Apply(this._SessionCollectionByAgentId, "AgentId", this.AgentId);
			}
		}
		
	}
	



	[Serializable]
	public partial class AgentMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected AgentMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(AgentMetadata.ColumnNames.PortalId, 0, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = AgentMetadata.PropertyNames.PortalId;
			c.NumericPrecision = 10;
			m_columns.Add(c);
				
			c = new esColumnMetadata(AgentMetadata.ColumnNames.AgentId, 1, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = AgentMetadata.PropertyNames.AgentId;
			c.IsInPrimaryKey = true;
			c.IsAutoIncrement = true;
			c.NumericPrecision = 10;
			m_columns.Add(c);
				
			c = new esColumnMetadata(AgentMetadata.ColumnNames.UserId, 2, typeof(System.Int32), esSystemType.Int32);
			c.PropertyName = AgentMetadata.PropertyNames.UserId;
			c.NumericPrecision = 10;
			m_columns.Add(c);
				
			c = new esColumnMetadata(AgentMetadata.ColumnNames.IsLoggedIn, 3, typeof(System.Boolean), esSystemType.Boolean);
			c.PropertyName = AgentMetadata.PropertyNames.IsLoggedIn;
			c.HasDefault = true;
			c.Default = @"((0))";
			m_columns.Add(c);
				
			c = new esColumnMetadata(AgentMetadata.ColumnNames.CreatedDate, 4, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = AgentMetadata.PropertyNames.CreatedDate;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
			c = new esColumnMetadata(AgentMetadata.ColumnNames.ModifiedDate, 5, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = AgentMetadata.PropertyNames.ModifiedDate;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public AgentMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string PortalId = "PortalId";
			 public const string AgentId = "AgentId";
			 public const string UserId = "UserId";
			 public const string IsLoggedIn = "IsLoggedIn";
			 public const string CreatedDate = "CreatedDate";
			 public const string ModifiedDate = "ModifiedDate";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string PortalId = "PortalId";
			 public const string AgentId = "AgentId";
			 public const string UserId = "UserId";
			 public const string IsLoggedIn = "IsLoggedIn";
			 public const string CreatedDate = "CreatedDate";
			 public const string ModifiedDate = "ModifiedDate";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(AgentMetadata))
			{
				if(AgentMetadata.mapDelegates == null)
				{
					AgentMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (AgentMetadata.meta == null)
				{
					AgentMetadata.meta = new AgentMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("PortalId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("AgentId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("UserId", new esTypeMap("int", "System.Int32"));
				meta.AddTypeMap("IsLoggedIn", new esTypeMap("bit", "System.Boolean"));
				meta.AddTypeMap("CreatedDate", new esTypeMap("datetime", "System.DateTime"));
				meta.AddTypeMap("ModifiedDate", new esTypeMap("datetime", "System.DateTime"));			
				
				
				
				meta.Source = "DNNspot_LiveChat_Agents";
				meta.Destination = "DNNspot_LiveChat_Agents";
				
				meta.spInsert = "proc_DNNspot_LiveChat_AgentsInsert";				
				meta.spUpdate = "proc_DNNspot_LiveChat_AgentsUpdate";		
				meta.spDelete = "proc_DNNspot_LiveChat_AgentsDelete";
				meta.spLoadAll = "proc_DNNspot_LiveChat_AgentsLoadAll";
				meta.spLoadByPrimaryKey = "proc_DNNspot_LiveChat_AgentsLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private AgentMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
